{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #训练计划 I"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #two-pointers #sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #双指针 #排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: trainingPlan"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #训练计划 I"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>教练使用整数数组 <code>actions</code> 记录一系列核心肌群训练项目编号。为增强训练趣味性，需要将所有奇数编号训练项目调整至偶数编号训练项目之前。请将调整后的训练项目编号以 <strong>数组</strong> 形式返回。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>actions = [1,2,3,4,5]\n",
    "<strong>输出：</strong>[1,3,5,2,4] \n",
    "<strong>解释：</strong>为正确答案之一</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>0 &lt;= actions.length &lt;= 50000</code></li>\n",
    "\t<li><code>0 &lt;= actions[i] &lt;= 10000</code></li>\n",
    "</ul>\n",
    "\n",
    "<p>&nbsp;</p>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof](https://leetcode.cn/problems/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof](https://leetcode.cn/problems/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,2,3,4,5]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        i, j = 0, len(actions) - 1\n",
    "        while i < j:\n",
    "            while i < j and actions[i] & 1 == 1: i += 1\n",
    "            while i < j and actions[j] & 1 == 0: j -= 1\n",
    "            actions[i], actions[j] = actions[j], actions[i]\n",
    "        return actions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        size = len(actions)\n",
    "        left, right = 0, size - 1\n",
    "        while left < right:\n",
    "            if actions[left] % 2 == 1:\n",
    "                left += 1\n",
    "            elif actions[right] % 2 == 0:\n",
    "                right -= 1\n",
    "            else:\n",
    "                actions[left], actions[right] = actions[right], actions[left]\n",
    "                left += 1\n",
    "                right -= 1\n",
    "        return actions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        #双指针\n",
    "        evenIndex = 0\n",
    "        oddIndex = len(actions) -1 \n",
    "        while evenIndex < oddIndex:\n",
    "            #evenIndex所指是偶数就向前走，直到指向奇数\n",
    "            while oddIndex > 0 and not (actions[oddIndex] % 2):\n",
    "                oddIndex -= 1\n",
    "            \n",
    "            #oddIndex指向奇数\n",
    "            while evenIndex < len(actions) and actions[evenIndex] % 2 == 1:\n",
    "                evenIndex += 1\n",
    "\n",
    "            #交换位置\n",
    "            if evenIndex < oddIndex:\n",
    "                actions[evenIndex] , actions[oddIndex] = actions[oddIndex], actions[evenIndex]\n",
    "\n",
    "            oddIndex -= 1\n",
    "            evenIndex += 1\n",
    "\n",
    "        return actions\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        fast, slow = 0, 0\n",
    "        while fast < len(actions):\n",
    "            if actions[fast] & 1 == 1:\n",
    "                actions[fast], actions[slow] = actions[slow], actions[fast]\n",
    "                slow += 1\n",
    "            fast += 1\n",
    "        \n",
    "        return actions\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: list[int]) -> list[int]:\n",
    "        prev = -1\n",
    "        # 遍历数组\n",
    "        for next in range(len(actions)):\n",
    "            # 元素对换临时变量\n",
    "            temp = 0\n",
    "            # 当前元素为奇数\n",
    "            if (actions[next]%2):\n",
    "                prev += 1\n",
    "                temp = actions[prev]\n",
    "                actions[prev] = actions[next]\n",
    "                actions[next] = temp\n",
    "        return actions \n",
    "\n",
    "if __name__ == '__main__':\n",
    "    actions = [1,2,3,4,5]\n",
    "    print(Solution().trainingPlan(actions))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        i, j = 0, len(actions) - 1\n",
    "        while i < j:\n",
    "            while i < j and actions[i] % 2 == 1:\n",
    "                i += 1\n",
    "            while i < j and actions[j] % 2 == 0:\n",
    "                j -= 1\n",
    "            actions[i], actions[j] = actions[j], actions[i]\n",
    "        return actions\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        n = len(actions)\n",
    "        i, j = 0 , n-1\n",
    "        while i <j:\n",
    "            while i<n and actions[i]%2 == 1:\n",
    "                i+=1\n",
    "            while j>=0 and actions[j]%2 == 0:\n",
    "                j-=1\n",
    "            if i<j and j>=0 and i<n:\n",
    "                actions[i], actions[j] = actions[j], actions[i]\n",
    "        return actions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        i, j = 0, len(actions)-1\n",
    "        while i<j:\n",
    "            while i<j and actions[i] % 2 == 1: i+=1\n",
    "            while i<j and actions[j] % 2 == 0: j-=1\n",
    "            actions[i], actions[j] = actions[j],actions[i]\n",
    "        return actions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: list[int]) -> list[int]:\n",
    "        prev = -1\n",
    "        # 遍历数组\n",
    "        for next in range(len(actions)):\n",
    "            # 元素对换临时变量\n",
    "            temp = 0\n",
    "            # 当前元素为奇数\n",
    "            if (actions[next]%2):\n",
    "                prev += 1\n",
    "                temp = actions[prev]\n",
    "                actions[prev] = actions[next]\n",
    "                actions[next] = temp\n",
    "        return actions "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        i,j = 0,len(actions)-1\n",
    "        while i < j:\n",
    "            while i < j and actions[i]&1 == 1: i +=1\n",
    "            while i < j and actions[j]&1 == 0: j-= 1\n",
    "            actions[i],actions[j] = actions[j],actions[i]\n",
    "        return actions\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "#         a=[]\n",
    "#         b=[]\n",
    "#         for i in range(0,len(actions)):\n",
    "#             if actions[i]%2==1:\n",
    "#                 a.append(actions[i])\n",
    "#             else :\n",
    "#                 b.append(actions[i])\n",
    "\n",
    "#         return a+b \n",
    "\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        i, j = 0, len(actions) - 1\n",
    "        while i < j:\n",
    "            while i < j and actions[i] & 1 == 1: i += 1\n",
    "            while i < j and actions[j] & 1 == 0: j -= 1\n",
    "            actions[i], actions[j] = actions[j], actions[i]\n",
    "        return actions\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        count = 0\n",
    "        for i in range(len(actions)):\n",
    "            if actions[i] % 2 == 1:\n",
    "                temp = actions.pop(i)\n",
    "                actions.insert(count, temp)\n",
    "                count += 1\n",
    "        \n",
    "        return actions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        i, j = 0, len(actions)-1\n",
    "        while i<j:\n",
    "            while i<j and actions[i] % 2 == 1: i+=1\n",
    "            while i<j and actions[j] % 2 == 0: j-=1\n",
    "            actions[i], actions[j] = actions[j],actions[i]\n",
    "        return actions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        i, j = 0, len(actions) - 1\n",
    "        while i < j:\n",
    "            while i < j and actions[i] & 1 == 1: \n",
    "                i += 1\n",
    "            while i < j and actions[j] & 1 == 0:\n",
    "                j -= 1\n",
    "            actions[i], actions[j] = actions[j], actions[i]\n",
    "        return actions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        i, j = 0, len(actions) - 1\n",
    "        while i < j:\n",
    "            while i < j and actions[i] & 1 == 1: i += 1\n",
    "            while i < j and actions[j] & 1 == 0: j -= 1\n",
    "            actions[i], actions[j] = actions[j], actions[i]\n",
    "        return actions\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        N=len(actions)\n",
    "        i,j=0,N-1\n",
    "        while i<j:\n",
    "            if actions[i]%2==0:\n",
    "                while(i<j and actions[j]%2==0):\n",
    "                    j-=1\n",
    "\n",
    "                if i<j and actions[j]%2==1:\n",
    "                    tmp=actions[j]\n",
    "                    actions[j]=actions[i]\n",
    "                    actions[i]=tmp\n",
    "                    j-=1\n",
    "\n",
    "            i+=1\n",
    "        return actions\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        l,r = 0,len(actions)-1\n",
    "        while l < r:\n",
    "            while l<r and actions[l]&1:\n",
    "                l += 1\n",
    "            while l<r and  not actions[r]&1:\n",
    "                r -= 1\n",
    "            if l<r :\n",
    "                actions[l], actions[r] = actions[r], actions[l]\n",
    "        return actions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        i = 0\n",
    "        j = len(actions) - 1\n",
    "        while i < j:\n",
    "            while i < j and actions[i] % 2 == 1:\n",
    "                i += 1\n",
    "            while i < j and actions[j] % 2 == 0:\n",
    "                j -= 1\n",
    "            actions[i], actions[j] = actions[j], actions[i]\n",
    "        return actions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        i,j=0,len(actions)-1\n",
    "        while i<j:\n",
    "            while i<j and actions[i]&1==1:i=i+1\n",
    "            while i<j and actions[j]&1==0:j=j-1\n",
    "            actions[i],actions[j]=actions[j],actions[i]\n",
    "            i,j=i+1,j-1\n",
    "        return actions\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        i, j = 0, len(actions)-1\n",
    "        print(i)\n",
    "        print(j)\n",
    "        while i < j:\n",
    "            while actions[i]%2 == 1 and i < j:\n",
    "                i += 1\n",
    "            while actions[j]%2 == 0 and i < j:\n",
    "                j -= 1\n",
    "            tmp = actions[i]\n",
    "            actions[i] = actions[j]\n",
    "            actions[j] = tmp\n",
    "        return actions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: list[int]) -> list[int]:\n",
    "        i, j = 0, len(actions) - 1\n",
    "        while i < j:\n",
    "            while i < j and actions[i] & 1 == 1: i += 1\n",
    "            while i < j and actions[j] & 1 == 0: j -= 1\n",
    "            actions[i], actions[j] = actions[j], actions[i]\n",
    "        return actions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        l = 0\n",
    "        r = len(actions)-1\n",
    "        while l<r:\n",
    "            while l<r and actions[l]&1==1:l += 1\n",
    "            while l<r and actions[r]&1==0:r -= 1\n",
    "            actions[l],actions[r]=actions[r],actions[l]\n",
    "        return actions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        '''\n",
    "        if actions == []:\n",
    "            return []\n",
    "        i = 0\n",
    "        j = len(actions)-1\n",
    "        while i != j:\n",
    "            while ( actions[i]%2 == 1 ) and (i != j):\n",
    "                i += 1\n",
    "            while ( actions[j]%2 == 0 ) and (i != j):\n",
    "                j -= 1\n",
    "            actions[i], actions[j] = actions[j], actions[i]\n",
    "        return actions\n",
    "        '''\n",
    "        i = 0\n",
    "        j = 0\n",
    "        for j in range(len(actions)):\n",
    "            if actions[j]%2 == 1:\n",
    "                actions[i], actions[j] = actions[j], actions[i]\n",
    "            if actions[i]%2 == 1:\n",
    "                i += 1\n",
    "            j += 1\n",
    "        return actions\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        left,right=0,len(actions)-1\n",
    "        while left<right:\n",
    "            while actions[left]&1==1 and left<right:left+=1\n",
    "            while actions[right]&1==0 and left<right:right-=1\n",
    "            actions[left],actions[right]=actions[right],actions[left]\n",
    "            left+=1\n",
    "            right-=1\n",
    "        return actions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        i,j =0,len(actions)-1\n",
    "        while i<j:\n",
    "          if i<j and actions[i]%2==1:i +=1;\n",
    "          if i<j and actions[j]%2==0:j -=1;\n",
    "          actions[i],actions[j]=actions[j],actions[i]\n",
    "        return actions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        i, j = 0, len(actions)-1\n",
    "        while i < j:\n",
    "            while i < j and not actions[j]%2: j-=1\n",
    "            while i < j and actions[i]%2: i+=1\n",
    "            actions[i], actions[j] = actions[j], actions[i]\n",
    "        return actions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        left, right = 0, len(actions)-1\n",
    "        while left<right:\n",
    "            while left < right and actions[left]%2!=0:\n",
    "                left+=1\n",
    "            while left < right and actions[right]%2==0:\n",
    "                right-=1\n",
    "            actions[left], actions[right] = actions[right], actions[left]\n",
    "        return actions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        i,j = 0,len(actions)-1\n",
    "        while i<j:\n",
    "            while i<j and actions[i] & 1 == 1: i += 1\n",
    "            while i<j and actions[j] & 1 == 0: j -= 1\n",
    "            actions[i],actions[j] = actions[j],actions[i]\n",
    "        return actions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        i,j = 0, len(actions)-1\n",
    "        while i<j:\n",
    "            while i<j and actions[i]%2!=0: i+=1\n",
    "            while i<j and actions[j]%2==0: j-=1\n",
    "            actions[i], actions[j] = actions[j], actions[i]\n",
    "        return actions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        left, right = 0, len(actions) - 1\n",
    "        while left < right:\n",
    "            while left < right and actions[left] % 2 == 1:\n",
    "                left += 1\n",
    "            while left < right and actions[right] % 2 == 0:\n",
    "                right -= 1\n",
    "            actions[left], actions[right] = actions[right], actions[left]\n",
    "            left += 1\n",
    "            right -= 1\n",
    "        return actions\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        i,j=0,len(actions)-1\n",
    "        while i<j:\n",
    "            while i<j and actions[i]&1==1:i=i+1\n",
    "            while i<j and actions[j]&1==0:j=j-1\n",
    "            actions[i],actions[j]=actions[j],actions[i]\n",
    "        return actions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        i, j = 0, len(actions) - 1\n",
    "        while i < j:\n",
    "            while i < j and actions[i] & 1 == 1: i += 1\n",
    "            while i < j and actions[j] & 1 == 0: j -= 1\n",
    "            actions[i], actions[j] = actions[j], actions[i]\n",
    "        return actions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        left, right = 0, len(actions) - 1\n",
    "        while left < right:\n",
    "            # 找到前半部分的偶数\n",
    "            while left < right and actions[left] % 2 == 1:\n",
    "                left += 1\n",
    "\n",
    "            # 找到后半部分的奇数\n",
    "            while left < right and actions[right] % 2 == 0:\n",
    "                right -= 1\n",
    "\n",
    "            # 交换\n",
    "            actions[left], actions[right] = actions[right], actions[left]\n",
    "            left += 1\n",
    "            right -= 1\n",
    "        return actions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        rlt=[]\n",
    "        for i in actions:\n",
    "            if i%2==0:\n",
    "                rlt.append(i)\n",
    "            else:\n",
    "                rlt.insert(0,i)\n",
    "        return rlt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        slow,fast=0,0\n",
    "        while fast<len(actions):\n",
    "            if actions[fast]&1==1:\n",
    "                actions[fast],actions[slow]=actions[slow],actions[fast]\n",
    "                slow+=1\n",
    "            fast+=1\n",
    "        return actions        \n",
    "\n",
    "            \n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        #双指针\n",
    "        evenIndex = 0\n",
    "        oddIndex = len(actions) -1 \n",
    "        while evenIndex < oddIndex:\n",
    "            #evenIndex所指是偶数就向前走，直到指向奇数\n",
    "            while evenIndex < oddIndex and not (actions[oddIndex] % 2):\n",
    "                oddIndex -= 1\n",
    "            \n",
    "            #oddIndex指向奇数\n",
    "            while evenIndex < oddIndex and actions[evenIndex] % 2 == 1:\n",
    "                evenIndex += 1\n",
    "\n",
    "            #交换位置\n",
    "            actions[evenIndex] , actions[oddIndex] = actions[oddIndex], actions[evenIndex]\n",
    "\n",
    "            oddIndex -= 1\n",
    "            evenIndex += 1\n",
    "\n",
    "        return actions\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        rlt=[]\n",
    "        for i in actions:\n",
    "            if i%2==0:\n",
    "                rlt.append(i)\n",
    "            else:\n",
    "                rlt.insert(0,i)\n",
    "        return rlt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        res = []\n",
    "        for i in range(len(actions)):\n",
    "            if actions[i] % 2 == 1:\n",
    "                res.append(actions[i])\n",
    "        for i in range(len(actions)):\n",
    "            if actions[i] % 2 == 0:\n",
    "                res.append(actions[i])\n",
    "        return res "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        p = [i for i in actions if i % 2 == 1]\n",
    "        q = [j for j in actions if j % 2 == 0]\n",
    "        p.extend(q)\n",
    "        return p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        ans = []\n",
    "        for n in actions:\n",
    "            if n%2==1:\n",
    "                ans.append(n)\n",
    "        for n in actions:\n",
    "            if n%2==0:\n",
    "                ans.append(n)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        s=[]\n",
    "        for i in range(len(actions)):\n",
    "            if actions[i]%2!=0:\n",
    "                s.append(actions[i])\n",
    "        for c in range(len(actions)):\n",
    "            if actions[c]%2==0:\n",
    "                s.append(actions[c])\n",
    "        return s\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "\n",
    "        order_list = []\n",
    "        \n",
    "        for i in actions:\n",
    "            if i%2==0:\n",
    "                order_list.append(i)\n",
    "            else:\n",
    "                order_list.insert(0,i)\n",
    "\n",
    "        return order_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "      p=[]\n",
    "      for i in actions:\n",
    "          if i%2==0:\n",
    "              p.insert(len(p),i)\n",
    "          else:\n",
    "              p.insert(0, i)\n",
    "      return p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        odds = []\n",
    "        evens = []\n",
    "        for action in actions:\n",
    "            if action % 2 == 1:\n",
    "                odds.append(action)\n",
    "            else:\n",
    "                evens.append(action)\n",
    "        return odds + evens"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        result = []\n",
    "        for i in actions:\n",
    "            if i % 2 == 0:\n",
    "                result.append(i)\n",
    "            else:\n",
    "                result.insert(0, i)\n",
    "        return result\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        ans=[]\n",
    "        for i in  actions:\n",
    "            if i %2==1:\n",
    "              ans.append(i)\n",
    "        for i in actions:\n",
    "            if i%2==0:\n",
    "                ans.append(i)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        a=len(actions)\n",
    "        b=[]\n",
    "        for i in range(a):\n",
    "            if actions[i]%2!=0:\n",
    "               b.append(actions[i])\n",
    "        for i in range(a):\n",
    "            if actions[i]%2==0:\n",
    "               b.append(actions[i])       \n",
    "        return b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        num1 = [x for x in actions if x % 2 == 0]\n",
    "        num2 = [x for x in actions if x % 2 == 1]\n",
    "        num2.extend(num1)\n",
    "        return num2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        n = len(actions)\n",
    "        res = []\n",
    "        for i in range(n):\n",
    "            if actions[i] % 2 == 1:\n",
    "                res.append(actions[i])\n",
    "        for i in range(n):\n",
    "            if actions[i] % 2 == 0:\n",
    "                res.append(actions[i])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        odd = [x for x in actions if x & 1]\n",
    "        even = [x for x in actions if x & 1 == 0]\n",
    "        return odd + even"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        odd = []\n",
    "        even = []\n",
    "        for i in actions:\n",
    "            if i%2 == 0:\n",
    "                even.append(i)\n",
    "            else :\n",
    "                odd.append(i)\n",
    "        return odd+even"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        even = []\n",
    "        odd = []\n",
    "        for i in range(len(actions)):\n",
    "            if actions[i]%2 == 0:\n",
    "                even.append(actions[i])\n",
    "            else:\n",
    "                odd.append(actions[i])\n",
    "        odd.extend(even)\n",
    "        return odd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        even = []\n",
    "        odd = []\n",
    "        for i in range(len(actions)):\n",
    "            if actions[i]%2 == 0:\n",
    "                even.append(actions[i])\n",
    "            else:\n",
    "                odd.append(actions[i])\n",
    "        odd.extend(even)\n",
    "        return odd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        a1=[]\n",
    "        a2=[]\n",
    "        for i in range(len(actions)):\n",
    "            if(actions[i]%2):\n",
    "                a1.append(actions[i])\n",
    "            else:\n",
    "                a2.append(actions[i])\n",
    "        return a1+a2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        actions.sort(key = lambda x: x%2,reverse = True)\n",
    "        return actions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        o_l = [i for i in actions if i%2]\n",
    "        e_l = [i for i in actions if not i%2]\n",
    "        return o_l+e_l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        j,o = [],[]\n",
    "        for i in actions:\n",
    "            if i%2 == 0 or i == 0:\n",
    "                o.append(i)\n",
    "            else:\n",
    "                j.append(i)\n",
    "        return j+o"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        a = []\n",
    "        b = []\n",
    "        for i in actions:\n",
    "            if i%2 == 0:\n",
    "                b.append(i)\n",
    "            else:\n",
    "                a.append(i)\n",
    "        return a + b\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    actions = [1,2,3,4,5]\n",
    "    print(Solution().trainingPlan(actions))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        odd = []\n",
    "        even = []\n",
    "        for i in actions:\n",
    "            if i%2 != 0:\n",
    "                odd.append(i)\n",
    "        for j in actions:\n",
    "            if j%2 == 0:\n",
    "                even.append(j) \n",
    "        return odd+even"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        if len(actions)<=0:return actions\n",
    "        act1=[]\n",
    "        act2=[]\n",
    "        for cell in actions:\n",
    "            if cell%2==0:\n",
    "                act2.append(cell)\n",
    "            else:\n",
    "                act1.append(cell)\n",
    "        for cell in act2:\n",
    "            act1.append(cell)\n",
    "        return act1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        l1 = []\n",
    "        l2 = []\n",
    "        for _ in actions:\n",
    "            if _ % 2 == 1:\n",
    "                l1.append(_)\n",
    "            else:\n",
    "                l2.append(_)\n",
    "        return l1+l2\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        # 奇偶排序问题\n",
    "        # 思路一: 把偶数取出，拼接到奇数上\n",
    "        temp1 = []\n",
    "        temp2 = []\n",
    "        for i in actions:\n",
    "            if i % 2 != 0:\n",
    "                temp1.append(i)\n",
    "            elif i % 2 == 0:\n",
    "                temp2.append(i)\n",
    "        return temp1 + temp2\n",
    "                \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        n=len(actions)\n",
    "        res=[0]*n\n",
    "        left,right=0,len(actions)-1\n",
    "        while left<right:\n",
    "            while left<right and actions[left]%2 != 0:left+=1\n",
    "            while left<right and actions[right]%2 == 0:right-=1\n",
    "            actions[left],actions[right]=actions[right],actions[left]\n",
    "        return actions\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        n = len(actions)\n",
    "        ans1 = []\n",
    "        ans2 = []\n",
    "        for i in actions:\n",
    "            if i%2 == 1:\n",
    "                ans1.append(i)\n",
    "            else:\n",
    "                ans2.append(i)\n",
    "        return ans1+ans2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        odd = []\n",
    "        even = []\n",
    "        for i in actions:\n",
    "            if i%2 != 0:\n",
    "                odd.append(i)\n",
    "            else: even.append(i)    \n",
    "        return odd+even"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        ls = []\n",
    "        ll = []\n",
    "        for i in actions:\n",
    "            if i % 2 !=0: \n",
    "                ls.append(i) \n",
    "            else: ll.append(i)\n",
    "        \n",
    "        lls = ls + ll \n",
    "        return lls\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        single = []\n",
    "        double = []\n",
    "        for i in actions:\n",
    "            if i%2 ==1:\n",
    "                single.append(i)\n",
    "            else:\n",
    "                double.append(i)\n",
    "        return single+double\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def trainingPlan(self, actions: List[int]) -> List[int]:\n",
    "        ls = []\n",
    "        ll = []\n",
    "        for i in actions:\n",
    "            if i % 2 !=0: \n",
    "                ls.append(i) \n",
    "            else: ll.append(i)\n",
    "        \n",
    "        lls = ls + ll \n",
    "        return lls"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
